package com.xnew.user.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.xnew.aes.LogParameter;
import com.xnew.aes.SecurityParameter;
import com.xnew.bean.common.SysUserInfo;
import com.xnew.constant.Constant;
import com.xnew.exception.CustomException;
import com.xnew.user.service.AuUserService;
import com.xnew.utils.DataInput;
import com.xnew.utils.DataOutput;
import com.xnew.utils.ResponseUtil;
import com.xnew.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;


@Controller
@RequestMapping("/sysuser-api")
public class UserController {
	@Autowired
	private AuUserService auUserService;
	
	/**
	 * @方法名：selectAll
	 * @功能说明：查询所有用户
	 * @author shic
	 * @date  2019年8月5日 下午4:39:22
	 * @param json
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping("/selectAll")
	@SecurityParameter
	public String selectAll(@RequestBody JSONObject json,HttpServletRequest request) {
		try {
			PageInfo<SysUserInfo> list = auUserService.selectAll(JSONObject.toJavaObject(json, Map.class), request);
			return ResponseUtil.responseMsg(list);
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseUtil.responseMsg("9999", "查询失败");
		}
	}
	
	/**
	 * @方法名：selectAll
	 * @功能说明：查询所有用户
	 * @author shic
	 * @date  2019年8月5日 下午4:39:22
	 * @param json
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping("/selectAllCustomer")
	@SecurityParameter
	public String selectAllCustomer(@RequestBody JSONObject json,HttpServletRequest request) {
		try {
			PageInfo<SysUserInfo> list = auUserService.selectAllCustomer(JSONObject.toJavaObject(json, Map.class), request);
			return ResponseUtil.responseMsg(list);
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseUtil.responseMsg("9999", "查询失败");
		}
	}
	
	@ResponseBody
	@RequestMapping("/cancelUser")
	@SecurityParameter
	public String cancelUser(@RequestBody JSONObject json,HttpServletRequest request) {
		try {
			auUserService.cancelAuUserId(JSONObject.toJavaObject(json, SysUserInfo.class), request);
			return ResponseUtil.responseMsg("0001", "修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseUtil.responseMsg("9999", "修改失败");
		}
	}
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping("/selectUserAll")
	@SecurityParameter
	public String selectUserAll(@RequestBody JSONObject json,HttpServletRequest request) {
		try {
			PageInfo<Map<String,Object>> list = auUserService.selectUserAll(JSONObject.toJavaObject(json, Map.class), request);
			return ResponseUtil.responseMsg(list);
		} catch (Exception e) {
			e.printStackTrace();
			throw new CustomException();
		}
	}
	
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping("/addUser")
	@SecurityParameter
	public String addUser(@RequestBody JSONObject json,HttpServletRequest request) {
		try {
			SysUserInfo user = auUserService.findUserByName(json.getString("loginName"));
			if(null != user) {
				return ResponseUtil.responseMsg("8888", "登录名重复");
			}
			auUserService.addUser(JSONObject.toJavaObject(json, Map.class), request);
			return ResponseUtil.responseMsg("0001", "保存成功");
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseUtil.responseMsg("9999", "系统错误");
		}
	}
	
	/**
	 * 删除user
	 */
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping("/delSysUserByUserId")
	@SecurityParameter
	public String delSysUserByUserId (@RequestBody JSONObject json,HttpServletRequest request) {
		try {
			auUserService.delSysUserByUserId(JSONObject.toJavaObject(json, Map.class), request);
			return ResponseUtil.responseMsg("0001", "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
//			throw new CustomException();
			return ResponseUtil.responseMsg("9999", "删除失败");
		}
	}
	
	/**
	 * 根据id查询user
	 */
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping("/queryUserByAuid")
	@SecurityParameter
	@LogParameter(key="userId", desc="根据id查询user")
	public String queryUserByAuid (@RequestBody JSONObject json,HttpServletRequest request) {
		try {
			SysUserInfo userMap=auUserService.findUserById(json.getString("auId"));
			return ResponseUtil.responseMsg(userMap);
		} catch (Exception e) {
			e.printStackTrace();
			throw new CustomException();
		}
	}
	


	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping("/updateRolesByAuid")
	@SecurityParameter
	public String updateRolesByAuid(@RequestBody JSONObject json,HttpServletRequest request) {
		try {
			SysUserInfo user = (SysUserInfo) request.getSession().getAttribute(Constant.LOGIN_USER);
			if(user == null) {
				throw new CustomException(9992,"登录人已失效");
			}
			auUserService.updateRolesByAuid(JSONObject.toJavaObject(json, Map.class), user);
			return ResponseUtil.responseMsg("0001", "修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			throw new CustomException();
		}
	}
	
	/**
	 * @方法名：queryInfoById
	 * @功能说明：首页名片
	 * @author yangyuan3
	 * @date  2019年9月7日 下午12:01:11
	 * @param json
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/queryInfoById")
	@SecurityParameter
	public String queryInfoById(@RequestBody JSONObject json, HttpServletRequest request) {
		try {
			SysUserInfo user = (SysUserInfo) request.getSession().getAttribute(Constant.LOGIN_USER);
			if(user == null) {
				throw new CustomException(9997,"用户信息为空");
			}
			Map<String,Object> ojb = auUserService.queryInfoById(user.getUserInfoNo());
			return ResponseUtil.responseMsg(ojb);
		} catch (Exception e) {
			e.printStackTrace();
			throw new CustomException();
		}
	}
	
	/**
	 * @方法名：updatePassword
	 * @功能说明：修改密码
	 * @author yangyuan3
	 * @date  2019年9月9日 下午3:51:02
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/updatePassword")
	@SecurityParameter
	public DataOutput updatePassword(@RequestBody DataInput input, HttpServletRequest request) {


		DataOutput dataOutput = DataOutput.success();
		try {
			SysUserInfo user = UserUtil.getUserInfo(request);
			if(user == null) {
				throw new CustomException(9997,"用户信息为空");
			}
			Object newPassword = input.getParam().get("newPassword");
			// 新密码
			user.setPwd(newPassword.toString());
			auUserService.updatePassword(user);
			return dataOutput;
			//return ResponseUtil.responseMsg("0001", "修改密码成功");
		} catch (Exception e) {
			e.printStackTrace();
			dataOutput.setCode(-99999);
			dataOutput.setMsg("修改密码失败");
			return dataOutput;
			//return ResponseUtil.responseMsg("9999", "修改密码失败");
		}
	}
	
	/**
	 * @方法名：resetPassword
	 * @功能说明：重置密码
	 * @author yangyuan3
	 * @date  2019年9月9日 下午4:05:32
	 * @param json
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/resetPassword")
	@SecurityParameter
	public String resetPassword(@RequestBody JSONObject json) {
		try {
			String userId = json.getString("userId");
			String loginName = json.getString("loginName");
			auUserService.resetPassword(userId, loginName);
			return ResponseUtil.responseMsg("0001", "重置密码成功");
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseUtil.responseMsg("9999", "重置密码失败");
		}
	}
	
    /**
     * 查询用户信息
     * */
    @RequestMapping("/searchUserAllInfo")
    @ResponseBody
    @SecurityParameter
    public String searchUserAllInfo(HttpServletRequest request) throws IOException {
		//生成主键编号
		try {
			SysUserInfo user = (SysUserInfo) request.getSession().getAttribute(Constant.LOGIN_USER);
			String userId = user.getUserInfoNo();
	    	Map<String,Object> map = auUserService.searchUserAllInfo(userId);
			return ResponseUtil.responseMsg(map);
		} catch (Exception e) {
			e.printStackTrace();
			throw new CustomException();
		}
    }
    
    /**
     * 修改用户昵称
     * */
    @RequestMapping("/updateUserNickName")
    @ResponseBody
    @SecurityParameter
    public String updateUserNickName(HttpServletRequest request) throws IOException {
		//生成主键编号
		try {
			SysUserInfo user = (SysUserInfo) request.getSession().getAttribute(Constant.LOGIN_USER);
			String userId = user.getUserInfoNo();
			String nickName = request.getParameter("nickName");
	    	auUserService.updateUserNickName(userId, nickName);
			return ResponseUtil.responseMsg("0001", "修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			throw new CustomException();
		}
    }
}
